﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using System.Threading.Tasks;
//using Kele.Api.Activity.Core;
//using Microsoft.AspNetCore.Http;
//using Newtonsoft.Json.Linq;
//using Qiniu.Storage;
//using Qiniu.Util;

//namespace Kele.Api.Activity.Engine
//{
//    public  class FileEngine
//    {
//        //Qiniu upload
//        public async Task<dynamic> CloudUpload(int merchant_id, IFormFile file)
//        {
//            var mac = new Mac(ApiConfig.KeleActivitySetting.QiniuAccessKey, ApiConfig.KeleActivitySetting.QiniuSecretKey);
//            // 存储空间名
//            var Bucket = ApiConfig.KeleActivitySetting.QiniuBucket;
//            // 设置上传策略，详见：https://developer.qiniu.com/kodo/manual/1206/put-policy
//            var putPolicy = new PutPolicy { Scope = Bucket };
//            putPolicy.SetExpires(3600 * 24);
//            var token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

//            var config = new Config
//            {
//                Zone = Zone.ZONE_CN_East,
//                UseHttps = true,
//                UseCdnDomains = true,
//                ChunkSize = ChunkUnit.U512K
//            };

//            // 上传文件名
//            var uid = ApiUtility.GenerateId();
//            var key = $"ump/merchant/{merchant_id}/{uid}";
//            var target = new FormUploader(config);
//            var url = ApiConfig.KeleActivitySetting.QiniuBaseUrl + key;
//            var result = await Task.Run(() => target.UploadStream(file.OpenReadStream(), key, token, null));

//            if (result.Code != 200) return null;
//            var json = JObject.Parse(result.Text);
//            //  var xKey = json.Value<string>("key");
//            //  var hash = json.Value<string>("hash");
//            return new
//            {
//                url,
//                name = file.FileName,
//                uid
//            };
//        }

//        public async Task QiniuDelete(int merchant_id, string uid)
//        {
//            var mac = new Mac(ApiConfig.KeleActivitySetting.QiniuAccessKey, ApiConfig.KeleActivitySetting.QiniuSecretKey);
//            var Bucket = ApiConfig.KeleActivitySetting.QiniuBucket;

//            var config = new Config { Zone = Zone.ZONE_CN_East };
//            var bucketManager = new BucketManager(mac, config);
//            var key = $"ump/merchant/{merchant_id}/{uid}";
//            var deleteRet = await Task.Run(() => bucketManager.Delete(Bucket, key));
//            if (deleteRet.Code != 200) throw new Exception(deleteRet.RefText);
//        }
//    }
//}
